<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	const SIDEBAR_TOP=1;
	const SIDEBAR_LEADING=2;
	const SIDEBAR_MAIN=3;
	const SIDEBAR_ADDITION=4;
	const SIDEBAR_END=5;

	/**
	 * @var string the default layout for the controller view. Defaults to 'application.views.layouts.column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='application.views.layouts.column2';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	private $_sidebarWidgets=array();

	public function init()
	{
		parent::init();
		$app=Yii::app();
		if($app->user->isGuest&&$app->request->getUrl()!='favicon.ico'
		&&strpos($app->request->getUrl(),'captcha')<0
		&&$app->createUrl($app->user->loginUrl[0])!=$app->request->getUrl()){
			$app->user->setReturnUrl($app->request->getUrl());
		}
		$cs=$app->clientScript;
		$cs->registerScriptFile($app->baseUrl.'/javascript/mcr_site.js');
		$cs->registerCoreScript('jquery');
		$this->_sidebarWidgets[self::SIDEBAR_TOP]=array();
		$this->_sidebarWidgets[self::SIDEBAR_LEADING]=array();
		$this->_sidebarWidgets[self::SIDEBAR_MAIN]=array();
		$this->_sidebarWidgets[self::SIDEBAR_ADDITION]=array();
		$this->_sidebarWidgets[self::SIDEBAR_END]=array();
		$this->addDefaultSidebarWidgets();
		$app->google->analytic("UA-1066686-2");
	}

	protected function addDefaultSidebarWidgets(){
		if(!Yii::app()->user->getIsGuest()){
			$this->addSidebarWidget('UserMenuPortlet');
		}
		if(rand()&1){
			$this->addSidebarWidget('GoogleAdsPortlet',array('adClientId'=>'ca-pub-8178908913858419','width'=>336,'height'=>280,'adSlot'=>'7819009657'));
		}else{
			$this->addSidebarWidget('AdZonePortlet',array('zone'=>1,'width'=>'336px','timeout'=>13400));
		}
		//$this->addSidebarWidget('AdZonePortlet',array('zone'=>4,'width'=>'336px','timeout'=>13400));
		//		if($user=Yii::app()->user){
		//			if(Yii::app()->user->checkAccess('admin',Yii::app()->user->getId())){
		//$this->addSidebarWidget('AdZonePortlet',array('zone'=>3,'width'=>'336px'), self::SIDEBAR_END);
		$this->addSidebarWidget('SimpleLinkPortlet',
		array('title'=>'Ads with MCR','description'=>'Put your ads on MCR Hotshot website!','link'=>array('site/page','view'=>'ads-with-mcr')),
		self::SIDEBAR_END);

		//			}
		//		}
		$this->addSidebarWidget('LatestHotshotsPortlet');
		$this->addSidebarWidget('QuickSearchPortlet',array(),self::SIDEBAR_LEADING);
		$this->addSidebarWidget('VoteTopPortlet');


	}

	public function addSidebarWidget($class,$param=array(),$order=NULL){
		if($order===NULL){
			$order=self::SIDEBAR_MAIN;
		}
		if(!isset($this->_sidebarWidgets[$order])){
			$this->_sidebarWidgets[$order]=array();
		}
		if($order===self::SIDEBAR_TOP){
			array_unshift($this->_sidebarWidgets[self::SIDEBAR_TOP],array($class,$param));
		}else{
			array_push($this->_sidebarWidgets[$order],array($class,$param));
		}
	}

	public function renderSidebarWidget(){
		$positions=array(self::SIDEBAR_TOP,self::SIDEBAR_LEADING,self::SIDEBAR_MAIN,self::SIDEBAR_ADDITION,self::SIDEBAR_END);
		foreach($positions as $pos){
			foreach($this->_sidebarWidgets[$pos] as $widget){
				$this->widget($widget[0],$widget[1]);
			}
		}
	}
}